const http = require("http");
const fs = require("fs");
const template = require("art-template");

const server = http.createServer();

var wwwDir = "D:/app/www";

server.on("request", (req, res) => {
    const url = req.url;

    fs.readFile("./template2.html", (err, data) => {
        if (err) {
            return res.end("404 Not Found")
        }

        fs.readdir(wwwDir, (err, files) => {
            if (err) {
                return res.end("Can not found www dir.")
            }
            // 用模板引擎解析替换  data 中的模板字符串
            const htm = template.render(data.toString(), {
                title: "123",
                files: files
            });
            // console.log(files)
            // 发送解析替换过的响应数据
            res.end(htm)
        });


    })

});

server.listen(2500, () => {
    console.log("this server is running at http://127.0.0.day01:2500")
});
